// 显示隐藏按钮
var register = document.querySelectorAll(".register-btn-apply")[1];
var window_top = 0;
window.addEventListener("scroll",function(){
    if(scroll().top>700){
        register.style.display = "block";
    }
    else{
        register.style.display = "none";
    }
    window_top = scroll().top;
})
register.addEventListener("click",function(){
    clearInterval(register.timer);
    register.timer = setInterval(function(){
        var step = -window_top/10;
        window_top += step;
        window.scrollTo(0,window_top);
        if(window_top<100){
            clearInterval(register.timer);
        }
    },20)
})



// ajax请求
var register_submit = $(".register-btn-apply").eq(0);
register_submit.click(function(){
    //定时器
	var timer = null;
	//60秒
	var i = 60;
	//记录this指向
	var _this = this;
	//正则中文验证
	var reg_name = /^[\u4e00-\u9fa5]{2,4}$/;
	//正则手机验证
	var reg_phone = /^1[3|4|5|8|7][0-9]\d{8}$/;
    // 正则收货地址验证
    var reg_address = /^(?=.*?[\u4E00-\u9FA5])[\dA-Za-z\u4E00-\u9FA5]+$/;
    regJudge(reg_name,reg_phone,reg_address,function(){
    //    $(_this).text("请稍等");
       var data = {
			"name" : $(".register-inp-value").eq(0).val(),
			"phone" : $(".register-inp-value").eq(1).val(),
			"address" : $(".register-inp-value").eq(2).val()
        };
        $.ajax({
            type: "post",
            url: "http://120.79.65.57:8080/api/services/app/Szkyjf/SubmitInfoNotSms",
            contentType : "application/json; charset=utf-8",
            data : JSON.stringify(data),
            dataType : "json",
            success: function(result){
                if(result.success){
                    layui.use('layer', function(){
						var layer = layui.layer;
						layer.msg(result.result.message);
                    })
                    $(_this).text("免费申领");
                }
            }
        })
    });
})

// 正则判断
function regJudge(reg_name,reg_phone,reg_address,fn){
    if(!reg_name.test($(".register-inp-value").eq(0).val())){
		layui.use('layer', function(){
			var layer = layui.layer;
			layer.msg("亲，名字是2到4个中文字符");
		});
	}
	//判断手机号码
	else if(!reg_phone.test($(".register-inp-value").eq(1).val())){
		layui.use('layer', function(){
			var layer = layui.layer;
			layer.msg("请输入正确的手机号码");
		});
	}
	else if(!reg_address.test($(".register-inp-value").eq(2).val())){
		layui.use('layer', function(){
			var layer = layui.layer;
			layer.msg("请输入正确的收货地址");
		});
    }
    else{
        fn();
    }
}
 
function scroll(){
    return {
        "top": window.pageYoffset || document.body.scrollTop || document.documentElement.scrollTop,
        "left": window.pageXoffset || document.body.scrollLeft || document.documentElement.scrollLeft
    }
}